//定义js命名空间
Ext.namespace('Infrastructure.InfraApprove');

//DateTime -- date , decimal -- float
//mapping : 'Department.Name'
//InfraApprove FieldArray
Infrastructure.InfraApprove.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'Name', type: 'string' },
	{ name: 'Code', type: 'string' },
	{ name: 'ApproveType', type: 'string' },
	{ name: 'DepartmentId', type: 'string' },
	{ name: 'Department.Name', mapping: 'Department.Name' },
	{ name: 'Department' },
	{ name: 'Isdelete', type: 'bool' },
	{ name: 'Description', type: 'string' },
	{ name: 'Version', type: 'int' },
	{ name: 'Createtime', type: 'date' }
);

//InfraApprove JsonReader
Infrastructure.InfraApprove.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraApprove.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: Infrastructure.InfraApprove.BaseFieldsArray
		}, cfg));
	}
});

//InfraApprove JsonStore
Infrastructure.InfraApprove.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.InfraApprove.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/InfraApprove/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields: Infrastructure.InfraApprove.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/InfraApprove/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//审批下拉
Infrastructure.InfraApprove.ApproveCombo = Ext.extend(Ext.ux.IdcComboBox, {
	constructor: function (cfg) {
		approveCombx = this;
		cfg = cfg || {};
		ApproveStore = new Infrastructure.InfraApprove.JsonStore();
		Infrastructure.InfraApprove.ApproveCombo.superclass.constructor.call(this, Ext.apply({
			store: ApproveStore,
			width: 150,
			listWidth: 200
		}, cfg));
		//重新设置当前值
		ApproveStore.load({
			callback: function () {
				cvalue = approveCombx.getValue();
				approveCombx.setValue(cvalue);
			}
		});
	}
});
Ext.reg('infra_approve_combo', Infrastructure.InfraApprove.ApproveCombo);

//InfraApprove GridPanel
Infrastructure.InfraApprove.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// Property
	//提交保存Url
	baseUrl: '/InfraApprove/',
	formSubmitUrl: '/InfraApprove/SaveOrUpdate',
	store: new Infrastructure.InfraApprove.JsonStore(),

	//initComponent
	initComponent: function () {
		//prepare
		var itemGrid = this;
		var cmdAddEntity = this.onAddEntity;

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [{
				dataIndex: 'Name',
				type: 'string'
			}, {
				dataIndex: 'Code',
				type: 'string'
			}, {
				dataIndex: 'ApproveType',
				type: 'list',
				options: Infrastructure.Params.DictFilter('approvetype')
			}, {
				dataIndex: 'DepartmentId',
				type: 'list',
				options: Infrastructure.Params.DepartmentFilter()
			}, {
				dataIndex: 'Isdelete',
				type: 'string'
			}, {
				dataIndex: 'Description',
				type: 'string'
			}, {
				dataIndex: 'Version',
				type: 'string'
			}, {
				dataIndex: 'Createtime',
				type: 'string'
			}]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			header: '',
			singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					itemGrid.delBtn.setDisabled(sm.getCount() < 1);
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{
					header: "流程名称",
					dataIndex: 'Name'
				}, {
					header: "流程代码",
					dataIndex: 'Code'
				}, {
					header: "分类",
					dataIndex: 'ApproveType',
					xtype: 'infra_dictcolumn',
					dictCode: 'ApproveType',
				}, {
					header: "展场",
					dataIndex: 'DepartmentId',
					xtype: 'infra_deptcolumn'
				}, {
					header: "描述",
					dataIndex: 'Description'
				}],
			defaults: {
				sortable: true,
				menuDisabled: false
			}
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		// 常用工具栏
		var toolBar = [{
			xtype: 'tbaraddbtn',
			handler: cmdAddEntity,
			scope: itemGrid
		}, '-', {
			xtype: 'tbardelbtn',
			handler: function () {
				itemGrid.delEntity();
			},
			scope: itemGrid
		}, '-', {
			xtype: 'textfield',
			width: 150,
			id: 'q_query'
		}, {
			xtype: 'tbarqrybtn',
			handler: function (e) {
				itemGrid.qryList(itemGrid.q_query.getValue());
			},
			scope: itemGrid
		}, '-'];

		Ext.apply(this, {
			plugins: [gridFilter],
			sm: selModel,
			cm: gridColumn,
			tbar: toolBar,
			bbar: pagebBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onLoadEntity(grid.store.getAt(rowIndex), true);
				}
			}
		});

		Infrastructure.InfraApprove.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onAddEntity: function () {
		var itemGrid = this;
		var itemEditForm = new Infrastructure.InfraApprove.EditForm({
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.InfraApprove.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function (selRecord, isEdit) {
		var itemGrid = this;
		var selId = selRecord.data.Id;
		var itemEditForm = new Infrastructure.InfraApprove.EditForm({
			itemId: selId,
			dataRecord: selRecord,
			isEdit: isEdit,
			waitMsg: '数据加载中...',
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
						JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.InfraApprove.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
	}
});

//InfraApprove EditForm
Infrastructure.InfraApprove.EditForm = Ext.extend(Infrastructure.BaseForm, {
	isEdit: true, //是否允许编辑
	submitUrl: '/InfraApprove/SaveOrUpdate',

	initComponent: function () {
		var itemEditForm = this;
		if (this.dataRecord != null)
			this.dataId = this.dataRecord.data.Id;

		//Form第一列
		// 定义表单列
		var firstColumn = [{
			fieldLabel: "流程名称",
			maxLength: 20,
			name: 'Name'
		}, {
			fieldLabel: "流程代码",
			maxLength: 20,
			name: 'Code'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: '流程类型',
			xtype: 'infra_localcombo',
			store: Infrastructure.Params.ApprTypeStore,
			hiddenName: 'ApproveType'
		}, {
			fieldLabel: '应用展场',
			xtype: 'deptcombo',
			hiddenName: 'DepartmentId',
			name: 'DepartmentId'
		}];

		//Form单列
		var singleColumn = [{
			xtype: 'textarea',
			fieldLabel: '描述',
			name: 'Description',
			height: 50,
			width: 430
		}];

		Ext.apply(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false
					},
					items: secondColumn
				}]
			}, singleColumn],
			buttons: [{
				xtype: 'btnsubmit',
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			}],
			reader: new Infrastructure.InfraApprove.JsonReader()
		});

		Infrastructure.InfraApprove.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');

		if (this.dataRecord != null) {
			this.form.loadRecord(this.dataRecord);
		}
	}
});

//InfraApprove DetailWin
Infrastructure.InfraApprove.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: '审批流程信息',
	iconCls: 'building_key',
	width: 600
});
